package com.ruoyi.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.system.domain.ArchiveResult;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.system.domain.vo.ArchiveResultVo;
import com.ruoyi.system.domain.bo.ArchiveResultBo;
import com.ruoyi.system.service.IArchiveResultService;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;

import java.util.List;
import java.util.Arrays;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.servlet.http.HttpServletResponse;

/**
 * 归档任务执行日志控制器
 * 前端访问路由地址为:/system/result
 *
 * @author ruoyi
 * @date 2024-12-06
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/result")
public class ArchiveResultController extends BaseController {

    private final IArchiveResultService iArchiveResultService;

    /**
     * 查询归档任务执行日志列表
     */
    @SaCheckPermission("system:result:list")
    @GetMapping("/list")
    public TableDataInfo<ArchiveResultVo> list(ArchiveResultBo bo, PageQuery pageQuery) {
        return iArchiveResultService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询归档任务执行日志列表
     */
    @SaCheckPermission("system:result:list")
    @GetMapping("/list/recover")
    public R<List<ArchiveResult>> list(String taskName) {
        List<ArchiveResult> list = iArchiveResultService.queryListForRecover(taskName);
        return R.ok(list);
    }

    /**
     * 导出归档任务执行日志列表
     */
    @SaCheckPermission("system:result:export")
    @Log(title = "归档任务执行日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ArchiveResultBo bo, HttpServletResponse response) {
        List<ArchiveResultVo> list = iArchiveResultService.queryList(bo);
        ExcelUtil.exportExcel(list, "归档任务执行日志", ArchiveResultVo.class, response);
    }

    /**
     * 获取归档任务执行日志详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:result:query")
    @GetMapping("/{id}")
    public R<ArchiveResultVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(iArchiveResultService.queryById(id));
    }

    /**
     * 新增归档任务执行日志
     */
    @SaCheckPermission("system:result:add")
    @Log(title = "归档任务执行日志", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ArchiveResultBo bo) {
        return toAjax(iArchiveResultService.insertByBo(bo));
    }

    /**
     * 修改归档任务执行日志
     */
    @SaCheckPermission("system:result:edit")
    @Log(title = "归档任务执行日志", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ArchiveResultBo bo) {
        return toAjax(iArchiveResultService.updateByBo(bo));
    }

    /**
     * 删除归档任务执行日志
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:result:remove")
    @Log(title = "归档任务执行日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iArchiveResultService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 导出归档结果
     *
     * @param id 主键
     */
    @SaCheckPermission("system:result:download")
    @GetMapping("/download/{id}")
    public void download(@NotNull(message = "主键不能为空") @PathVariable Long id, HttpServletResponse response) {
        try {
            iArchiveResultService.download(id, response);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
